package ioperation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书名");
        String name = scanner.nextLine();
        //找到这本书
        int pos = -1;
        int currentSize = bookList.getUsedSize();
        for(int i = 0;i<currentSize;i++){
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                pos = i;
                break;
            }
            if(currentSize == i){
                System.out.println("书架上没有这本书");
                return;
            }

        }
        //覆盖式删除
        for(int i = pos;i<currentSize;i++){
            Book book = bookList.getBook(i+1);
            bookList.setBook(i,book);
        }
        bookList.setBook(currentSize-1,null);//去掉重复的成员
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");
    }
}
